import Vue from 'vue'
import co from 'co'
// import CommonService from './CommonService'
import * as Util from '../components/Util'

// 收费冲正
let sellCancelGen = function * (model, row) {
  try {
    // 存库
    let result = yield co(cardChargeCancelGen(model, row))

    // 获取气价和售气情况参数
    let cardParams = yield co(getParamGen(row.f_card_id))

    // 写卡
    let params = {
      factory: row.f_alias,                                // 气表厂家
      kh: row.f_card_id,                                   // 卡号
      yhh: row.cardInfo.Meterid,                           // 用户号，档案中自己输
      kmm: row.f_card_password,                            // 卡密码，写卡后返回新密码, 不论补卡还是先开户都没有密码
      bjql: row.f_police_gas,                              // 报警气量,t_gasbrand表中
      czsx: row.f_topup_ceil,                              // 充值上线，t_gasbrand表中
      tzed: row.f_overdr_lines,                            // 透支额度，t_gasbrand表中
      bkcs: model.f_replace_time,                          // 补卡次数，从补卡表里通过表编号获得
      ql: '0',                                             // 预购气量
      csql: cardParams.data.sc,                            // 上次购气量，新开户为0
      ccsql: cardParams.data.ssc,                          // 上上次购气量，新开户为0
      cs: row.f_times - 1,                                     // 求购气次数，先开户为1
      ljgql: cardParams.data.f_pregas,                     // 当前累计购气量
      ljyql: cardParams.data.f_pregasall,                  // 累积用气量，有些表要累加原来用气量,新开户卡，为0
      stairamount: cardParams.data.stairamount,            // 旧单价，价格管理中取
      stairprice: cardParams.data.stairprice,              // 新单价，价格管理中取
      money: model.f_preamount,                            // 购气金额
      totalmoney: cardParams.data.f_collection             // 累计金额
    }

    // if(row.cardInfo.Meterid && row.cardInfo.cs === 1) {
    //   params.yhh = row.cardInfo.Meterid
    // }

    let cardRes = yield co(Vue.resetpost('http://127.0.0.1:8003/WriteGasCard', params, {resolveMsg: null, rejectMsg: '冲正卡上气量卡服务错误'}))
    // 卡服务写卡异常，业务逻辑回滚
    if (cardRes.data.Err || cardRes.data.Exception) {
      let data = {
        id: result.data.id,
        userinfo: {
          f_userinfo_id: row.f_userinfo_id,
          version: row.userinfo_version + 1,
          f_balance: row.f_balance,
          userfiles: {
            f_user_id: model.f_user_id,
            f_meter_base: row.f_meter_base,
            version: row.version + 1
          }
        },
        changeplus: {
          f_user_id: model.f_user_id,
          f_meter_base: row.f_meter_base + model.f_pregas,
          f_remanent_gas: row.f_remanent_gas - model.f_pregas,
          version: row.version + 1
        },
        table_id: model.id
      }
      yield Vue.resetpost('rs/logic/cancelRollback', {data: data}, {resolveMsg: '冲正卡上气量成功', rejectMsg: '冲正失败'})
    } else {
      Vue.showAlert('撤销成功', 'success', 2000)
    }
  } catch (error) {
    Vue.console.error('出错', error)
  }
}

let cardChargeCancelGen = function * (model, row) {
  let data = {
    cancel: {
      f_user_id: row.f_user_id,
      f_user_name: row.f_user_name,
      f_correct_type: '',
      f_correct_id: model.id,
      f_correct_cause: '购气错误',
      f_correct_money: 0 - model.f_preamount,
      f_correct_state: '有效',
      f_correct_gas: 0 - model.f_pregas,
      f_operator: Util.f.name
    },
    userinfo: {
      f_userinfo_id: row.f_userinfo_id,
      version: row.userinfo_version,
      f_balance: model.f_balance + model.f_collection,
      userfiles: {
        f_user_id: model.f_user_id,
        f_meter_base: row.f_meter_base - model.f_pregas,
        f_times: row.f_times - 1,
        version: row.version
      }
    },
    changeplus: {
      f_user_id: model.f_user_id,
      f_meter_base: row.f_meter_base - model.f_pregas,
      f_remanent_gas: row.f_remanent_gas + model.f_pregas,
      version: row.version
    },
    type: model.f_canceltype,
    id: model.id
  }
  return Vue.resetpost('rs/logic/chargeCancel', {data: data}, {resolveMsg: null, rejectMsg: '冲正卡上气量存库失败'})
}

let getParamGen = function * (id) {
  let data = {
    f_card_id: id + ''
  }
  return co(Vue.resetpost('rs/logic/stairCard', {data: data}, {resolveMsg: null, rejectMsg: '获取写卡参数失败'}))
}

let recordCancelGen = function * (model, data) {
  // 获取表档案和户档案参数
  let result = yield co(Vue.resetpost('rs/logic/cardChargeCancel', {data: {model: model, recordData: data}}, {resolveMsg: null, rejectMsg: '获取写卡参数失败'}))

  // 防止表档案和户档案版本号不明确
  result.data.userinfo.userinfo_version = result.data.userinfo.versioin
  // 对数据进行整理
  yield co(sellCancelGen(result.data.sell, Object.assign({}, result.data.userinfo, result.data.userfile)))
}

let CancelService = {
  install (Vue, options) {
    // 给vue增添对话框显示方法
    Vue.CancelService = Vue.prototype.$CancelService = CancelService
  },
  cardChargeCancel (model, row) {
    let cardGen = sellCancelGen(model, row)
    return co(cardGen)
  },
  // 撤销操作
  cancel (url, model, data) {
    // TODO 还有换表补气，和掉气补气两个，（需要将record表中类型换成对应的补气类型）
    if (data.type === '卡表收费' || data.type === '发卡售气') {
      let recordGen = recordCancelGen(model, data)
      return co(recordGen)
    } else {
      return Vue.resetpost(url, {data: {model: model, recordData: data}}, {resolveMsg: '撤销成功', rejectMsg: '撤销失败'})
    }
  }
}

export default CancelService
